package org.arclib;

import java.io.File;
import java.io.IOException;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.arclib.draw.Draw;
import org.arclib.math.Size;
import org.arclib.time.Time;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;

public class ArcGame
{
	protected ArcGame(String argT, Size argS, boolean argFS) throws IOException
	{
		// delete log if exists
		File f = new File("arc.log");
		if (f.exists())
		{
			f.delete();
		}

		// set up logging
		BasicConfigurator.configure();
		BasicConfigurator.configure(new FileAppender(new PatternLayout(
				PatternLayout.TTCC_CONVERSION_PATTERN), "arc.log"));

		window = new Window(argT, argS, argFS);
		draw = new Draw();
		loader = new ResourceLoader();
		time = new Time();
	}

	// user will write render code here
	public void process()
	{

	}

	private void shutdown()
	{

	}

	public void run() throws InterruptedException
	{
		while (!Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)
				&& !Display.isCloseRequested())
		{
			Window.clear();
			time.process();
			time.limitFPS(targetFPS);
			process();
			Window.swap();
		}

		shutdown();
	}

	void setTargetFPS(int argFPS)
	{
		targetFPS = argFPS;
	}

	// Window system
	protected Window window;

	// Drawing System
	protected Draw draw;

	// Resource loader
	protected ResourceLoader loader;

	// time
	protected Time time;

	// target fps
	protected int targetFPS = -1;

	// Logging system
	protected static Logger log = Logger.getLogger(ArcGame.class);
	// private FPS fps;
}
